﻿#region Using Statements

using Microsoft.Xna.Framework.Input;

#endregion

namespace BrickBreaker.Core
{
    public enum PressedState { SINGLE_PRESS, LONG_PRESS };

    public class BBKeyboard
    {
        //Data members or instance variables....
        private KeyboardState CurrentKeyboardState, LastKeyboardState;
        private PressedState pressedState;

        /// <summary>
        /// Called in an Update method. Updates the state of the keyboard and all it's keys.
        /// </summary>
        public void Update()
        {
            LastKeyboardState = CurrentKeyboardState;
            CurrentKeyboardState = Microsoft.Xna.Framework.Input.Keyboard.GetState();
        }// end Update()



        #region CHECK-KEYPRESS-STATE
        //
        /// <summary>
        /// Checks whether a key on the keyboard is pressed down.
        /// Used for continuous action such as repeated gunfire, etc.
        /// </summary>
        /// <param name="keys">The key being checked if pressed down or not.</param>
        //Check if Key is CONTINUOUSLY Pressed - Action repeatedly occurs...
        private bool IsKeyPress(Keys keys)
        {
            return CurrentKeyboardState.IsKeyDown(keys);
        }

        /// <summary>
        /// Checks whether a key being pressed is a new key press.
        /// For example: pressed, released, then pressed again.
        /// Used for single actions such as jump, etc.
        /// </summary>
        /// <param name="sprite">The object being checked for collision.</param>
        //One action per Key Press - Release and Press Again for action...
        private bool IsNewKeyPress(Keys keys)
        {
            return (LastKeyboardState.IsKeyUp(keys) && (CurrentKeyboardState.IsKeyDown(keys)));
        }
        //
        #endregion CHECK-KEYPRESS-STATE




        #region KEYBOARD PRESSED_STATE
        //
        public PressedState KeyPressedState
        {
            get { return pressedState; }
            set { pressedState = value; }
        }
        //
        #endregion KEYBOARD PRESSED_STATE




        #region SPECIFIC_KeyPress
        //
        public bool Escape
        {
            get
            {
                if (pressedState == PressedState.SINGLE_PRESS)
                {
                    //Update();
                    return IsNewKeyPress(Keys.Escape);
                }
                else
                {
                    //Update();
                    return IsKeyPress(Keys.Escape);
                }
            }
        }

        //
        public bool Enter
        {
            get
            {
                if (pressedState == PressedState.SINGLE_PRESS)
                {
                    //Update();
                    return IsNewKeyPress(Keys.Enter);
                }
                else
                {
                    //Update();
                    return IsKeyPress(Keys.Enter);
                }
            }
        }

        //
        public bool Spacebar
        {
            get
            {
                if (pressedState == PressedState.SINGLE_PRESS)
                {
                    //Update();
                    return IsNewKeyPress(Keys.Space);
                }
                else 
                {
                    //Update();
                    return IsKeyPress(Keys.Space);
                }
            }
        }

        //
        public bool Up
        {
            get
            {
                if (pressedState == PressedState.SINGLE_PRESS)
                {
                   // Update();
                    return IsNewKeyPress(Keys.Up);
                }
                else
                {
                    //Update();
                    return IsKeyPress(Keys.Up);
                }
            }
        }

        //
        public bool Down
        {
            get
            {
                if (pressedState == PressedState.SINGLE_PRESS)
                {
                    //Update();
                    return IsNewKeyPress(Keys.Down);
                }
                else
                {
                    //Update();
                    return IsKeyPress(Keys.Down);
                }
            }
        }

        //
        public bool Left
        {
            get
            {
                if (pressedState == PressedState.SINGLE_PRESS)
                {
                    //Update();
                    return IsNewKeyPress(Keys.Left);
                }
                else
                {
                    //Update();
                    return IsKeyPress(Keys.Left);
                }
            }
        }

        //
        public bool Right
        {
            get
            {
                if (pressedState == PressedState.SINGLE_PRESS)
                {
                    //Update();
                    return IsNewKeyPress(Keys.Right);
                }
                else
                {
                    //Update();
                    return IsKeyPress(Keys.Right);
                }
            }
        }

        public bool Pause
        {
            get
            {
                //if (pressedState == PressedState.SINGLE_PRESS)
                //{
                    //Update();
                    return IsNewKeyPress(Keys.P);
                //}
                //else
                //{
                    //Update();
                  //  return IsKeyPress(Keys.P);
                //}
            }
        }

        //
        public bool BackSpace
        {
            get
            {
                if (pressedState == PressedState.SINGLE_PRESS)
                {
                    //Update();
                    return IsNewKeyPress(Keys.Back);
                }
                else
                {
                    //Update();
                    return IsKeyPress(Keys.Back);
                }
            }
        }
        //
        #endregion SPECIFIC_KeyPress

        
    }//end class
}//end namespace
